import { DatabaseHelper } from './DatabaseHelper.js';

let myHandler = async function (event, context, callback, logger) {
  logger.info(JSON.stringify(event));
  let userid: string = event.body ? JSON.parse(event.body).userId : event.userId;
  let type: string = event.body ? JSON.parse(event.body).type : event.type;
  let msg;

  if(!userid||!type){
    callback({
      code: 2,
      message: 'userid或type不能为空',
      data: null,
    });
  }

  try {
    const databaseHelper = new DatabaseHelper(context, logger);
    const result = await databaseHelper.queryTopics(userid,type);
    if (result) {
      callback({
        code: 0,
        message: 'Success',
        data: [...result],
      });
    } else {
      callback({
        code: 1,
        message: '未找到数据',
        data: null,
      });
    }
  } catch (err) {
    logger.error("func error:" + err.message + " stack:" + err.stack);
    callback({
      code: 3,
      message: '数据查询失败',
      data: err,
    });
  }


};

export { myHandler };